//
//  LKPictureView.swift
//  新浪微博
//
//  Created by 胡不归是大虾 on 16/9/27.
//  Copyright © 2016年 胡不归是大虾. All rights reserved.
//

import UIKit

class LKPictureView: UICollectionView {
    var pic_urls : [LKPic_urls]? {
        didSet{
            //数据源一赋值就刷新界面
            reloadData()
        }
    }
    override func awakeFromNib() {
        self.dataSource = self
        //注册cell
        self.register(LKPictureViewCell.self, forCellWithReuseIdentifier: LKPictureViewCellID)
    }
}
extension LKPictureView : UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return pic_urls?.count ?? 0
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: LKPictureViewCellID, for: indexPath) as! LKPictureViewCell
        cell.pic_url = pic_urls![indexPath.item]
        return cell
    }
}

class LKPictureViewCell: UICollectionViewCell {
    //添加一个imageView
    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(imageView)
        addSubview(gifIcon)
        //设置图片约束
        imageView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        gifIcon.snp.makeConstraints { (make) in
            make.bottom.right.equalToSuperview()
        }
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //添加一个模型
    var pic_url : LKPic_urls? {
        didSet{
            let picURLString = pic_url?.wap_pic ?? ""
//            print(pic_url?.wap_pic)
//            print("💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗")
            let url = URL(string: picURLString)
            imageView.sd_setImage(with: url)
            gifIcon.isHidden = !url!.absoluteString.hasSuffix(".gif")
        }
    }
    //懒加载子控件
    lazy var imageView : UIImageView = {
        let imageView = UIImageView()
        imageView.contentMode = .scaleAspectFill
        imageView.clipsToBounds = true
        return imageView
    }()
    //gif图标懒加载
    private lazy var gifIcon: UIImageView = UIImageView(image:#imageLiteral(resourceName: "timeline_image_gif") )
    
}








